﻿using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Text;

namespace Acoris.Document
{
    /// <summary>
    /// 
    /// </summary>
    public class DelegateElement : IContentSegment
    {
        private object m_lock = new object();
        protected ResourceKey SourceKey;
        private IContentFormat m_format;

        protected DelegateElement(ResourceKey source)
        {
            SourceKey = source ?? throw new ArgumentNullException();
        }

        /// <summary>
        /// 
        /// </summary>
        public object Synchronize {
            get { return m_lock; }
        }

        /// <summary>
        /// 
        /// </summary>
        public IContentFormat Format {
            get { return m_format; }
        }

        /// <summary>
        /// 
        /// </summary>
        public int Length { get => throw new NotImplementedException(); }

        /// <summary>
        /// 
        /// </summary>
        public ResourceKey Source {
            get { return SourceKey; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="format"></param>
        public void ApplyFormat(IContentFormat format)
        {
            Debug.Assert(format != null);
            m_format = format;
        }

        public virtual void RemoveRange(int offset, int count)
        {
            throw new NotImplementedException();
        }

        public virtual void ReplaceRange(int offset, int count, IContentSegment content)
        {
            throw new NotImplementedException();
        }

        public virtual void InsertRange(int offset, IContentSegment content)
        {
            throw new NotImplementedException();
        }

        public virtual IContentSegment Split(int offset)
        {
            throw new NotImplementedException();
        }

        public void ClearFormat()
        {
            m_format = null;
        }
    }

    /// <summary>
    /// 表示一个交互元素段
    /// </summary>
    public class InteractionSegment : DelegateElement
    {
        private object m_lock = new object();
        private ResourceKey m_targetKey;

        #region Constructor

        protected InteractionSegment(ResourceKey source):base(source) { }

        internal InteractionSegment(ResourceKey source,ResourceKey target):base(source)
        {
            m_targetKey = target;
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        public ResourceKey Target {
            get { return m_targetKey; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="content"></param>
        public override void InsertRange(int offset, IContentSegment content)
        {
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        public override void RemoveRange(int offset, int count)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <param name="count"></param>
        /// <param name="content"></param>
        public override void ReplaceRange(int offset, int count, IContentSegment content)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="offset"></param>
        /// <returns></returns>
        public override IContentSegment Split(int offset)
        {
            throw new NotImplementedException();
        }

        #region Override

        /// <summary>
        /// 获取与当前对象等价的字符串形式
        /// </summary>
        /// <returns>返回与当前对象等价的字符串形式</returns>
        public override string ToString()
        {
            return "Interaction";
        }

        /// <summary>
        /// 获取当前对象与指定对象之间的等价性
        /// </summary>
        /// <param name="obj">指定要比较的对象</param>
        /// <returns>如果两个对象之间等价则返回true，否则返回false</returns>
        public override bool Equals(object obj)
        {
            if (obj is InteractionSegment interaction) {
                return interaction.SourceKey == this.SourceKey && interaction.m_targetKey == this.m_targetKey;
            }
            return false;
        }

        /// <summary>
        /// 获取当前对象的哈希值
        /// </summary>
        /// <returns>返回当前对象的哈希值</returns>
        public override int GetHashCode()
        {
            return base.GetHashCode() ^ this.SourceKey.GetHashCode();
        }

        #endregion
    }
}
